package com.gz.furnitures.web.throwedServlet;

import com.gz.furnitures.entity.Member;
import com.gz.furnitures.service.impl.MemberServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(urlPatterns = "/loginServlet")
public class LoginServlet extends HttpServlet {
    private MemberServiceImpl memberService = new MemberServiceImpl();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("LoginServlet 的dopost");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        Member member = memberService.login(new Member(null, username, password, null));
        if(member != null) {
            System.out.println("登陆成功");
        }else {
            //验证失败将用户名和错误信息返回
            request.setAttribute("msg","用户名或者密码错误");
            request.setAttribute("username",username);
            //返回登录页面
            request.getRequestDispatcher("/views/member/login.jsp").forward(request,response);
            System.out.println("登录失败");
        }
    }
}
